nexus和harbor nexus和harbor哪个更好

您所在的位置:网站首页 harbour port区别 nexus和harbor nexus和harbor哪个更好

nexus和harbor nexus和harbor哪个更好

2024-06-13 23:08| 来源: 网络整理| 查看: 265

容器化的环境下,公司内部使用的镜像不可能直接放在 dockerhub 上,更不可能只放在某台机器上,所以一个公司内部的私有的 docker 镜像仓库(registry)必须存在。

目前来讲,docker registry 的实现还蛮多,包括官方的 registry、harbor,以及 nexus 等。

本文会讲 nexus 和 harbor 的使用,为什么要使用两个而不是其中一个呢?因为它们都有缺陷,两者合一用起来才完美。

harbor 的优势很明显,特别是可以自建文件夹进行分组这点就非常好。其实说实话,作为一个私有的镜像仓库,harbor 已经做得很好的了,唯一的缺点是它无法帮你下载镜像。

也可能并不是缺点,只是定位不同,而在这一点上 nexus 就很好的做到了,但是 nexus 除了这点外,其他好像没什么值得称道的地方了。

为什么需要 Registry 帮你下载镜像呢?因为在 kubernetes 环境下,你肯定有去公网拉镜像的需求,无论是官方还是非官方。你不可能因为这个而特地给你的所有 node 节点开通外网访问吧,这样风险太多且不可控。在我看来,整个 kubernetes 集群都不能访问外网。

这时 nexus 就有了用武之地了,当你需要拉公网镜像的时候,你只要向它发起请求,它如果本地没有,就会自动去你配置的镜像仓库下载,下载完成之后先在本地缓存一份,然后发送给你。

你甚至不用给 nexus 开通外网,只需要在一台可以访问外网的机器上搭建一个代理服务就行,让 nexus 通过代理去下载。那这方面的内容,我之前的文章中已经给出了解决方法,欢迎查看。

好了,关于 nexus 和 harbor 对比的简单说明就这么多了。那么本文的目标就是搭建 nexus 和 harbor,并使用免费可靠的 https 证书来开启 https。

nexus nexus 最初是给 java 用的,后来慢慢发展,可以作为众多软件的仓库,比如 pipy、yum 等,在 3.0 版本中更是加入了 docker registry 的支持,因此我们要使用的至少是 3.0 版。

nexus 提供了 web 界面,默认监听 8081 端口,我们几乎所有的操作都可以在 web 界面上完成。当 docker 连接 nexus 时,默认通过 https,但是这只是针对 docker 仓库的端口(需要配置),而不是针对 web 界面。

也就是说,docker 并不会连接 web 界面所监听的 8081 端口,因此我们无需对 web 界面进行 https 配置。但是话又说回来,反正都要配 https 了,那将 web 界面进行 https 也只是顺手的事。

说了这么多,我们正式开始搭建。nexus 版本从 3 开始才支持 docker registry,这点需要注意,我们就直接下载最新版就好了。你可以直接访问官方文档来全面了解它。

安装 nexus 支持 docker 部署,我们这里就不用了,直接使用一台 Linux(CentOS7)搞起。

首先需要安装 jdk,官方说不支持 openjdk,只支持 oracle jdk。那么直接去 oracle 官网下载最新版的 rpm 包,放到服务器上使用 rpm -ivh 进行安装即可:

curl -v -j -k -L -H “Cookie: oraclelicense=accept-securebackup-cookie” https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.rpm >jdk-8u201-linux-x64.rpm rpm -ivh jdk-8u201-linux-x64.rpm

复制代码 下载 jdk 完成后会出现下面这样信息,但这并不是报错。

Connection #0 to host download.oracle.com left intact 复制代码 接着下载并解压 nexus:cd /opt wget http://download.sonatype.com/nexus/3/nexus-3.15.2-01-unix.tar.gz tar xf nexus-3.15.2-01-unix.tar.gz ln -s nexus-3.15.2-01/ nexus

当我们使用 root 用户启动时,虽然也能成功启动,但是它会抛出警告信息:

cd nexus/bin ./nexus start WARNING: ************************************************************ WARNING: Detected execution as “root” user. This is NOT recommended! WARNING: ************************************************************ Starting nexus 复制代码 因此我们需要创建一个普通用户来启动它:./nexus stop groupadd -g 1111 nexus useradd -u 1111 -g 1111 nexus chown -R nexus. /opt/sonatype-work/ su - nexus $ /opt/nexus/bin/nexus start 复制代码 可以验证下它是否启动成功:$ ss -tnl | grep 8081 LISTEN 0 50 *:8081 :

复制代码 简单配置 我们可以在 /opt/nexus/bin 下面看到一个 nexus.vmoptions 的配置文件,通过这个配置文件可以直接 jvm 参数。

$ cat nexus.vmoptions -Xms1200M -Xmx1200M -XX:MaxDirectMemorySize=2G -XX:+UnlockDiagnosticVMOptions -XX:+UnsyncloadClass -XX:+LogVMOutput -XX:LogFile=…/sonatype-work/nexus3/log/jvm.log -XX:-OmitStackTraceInFastThrow -Djava.net.preferIPv4Stack=true -Dkaraf.home=. -Dkaraf.base=. -Dkaraf.etc=etc/karaf -Djava.util.logging.config.file=etc/karaf/java.util.logging.properties -Dkaraf.data=…/sonatype-work/nexus3 -Djava.io.tmpdir=…/sonatype-work/nexus3/tmp -Dkaraf.startLocalConsole=false

复制代码 这里的相对路径是相对于 nexus 本身,而非 nexus/bin。我们可以看到它会自动在 nexus 目录的父目录下生成 sonatype-work 目录,用来放配置文件、数据,还有日志等,你可以通过修改配置文件来改变它的位置。

前面使用 root 用户启动之后生成的这个目录的权限是 root,再使用普通用户启动的话,普通用户会因为没有权限往里面的日志文件中写数据而导致启动失败,所以只是上面要修改这个目录权限的原因。

配置 https 此时你可以直接使用浏览器访问 http://ip:8081 来打开它的 web 界面了,之后我们所有的操作都会在 web 界面上进行。在配置之前,我们先将 https 配置上去。之所以使用 https,是因为 docker 访问 registry 默认就是使用 https,如果使用 http 也行,但是会增加很多操作。

nexus 使用 https 的方式有两种,一种是直接配置 nexus 使用 https,另一种就是前面使用 nginx 配置 https,然后反向代理到后端的 nexus,其中客户端和 nginx 之间使用 https,nginx 和 nexus 之间则使用 http。而不管是从易用性还是实用性的角度,我们都应该使用 nginx。

其实我们要使用 https 的地方在于 docker 和 registry 之间,而非 nexus 的 web 界面。docker 和 registry 之间通信会使用另外的端口,而且 pull 和 push 端口都不一样,这个下面会讲到。

但是既然都要配 https 了,我们将 web 访问也配上 https 也只是顺手的事,反正证书都是使用同一个,而且使用 https 逼格更高。

OK,我们先安装 nginx。

cat >/etc/yum.repos.d/nginx.repo docker(proxy):

Name:docker-hub; Online:勾选; remote storage:https://registry-1.docker.io,这是 dockerhub 的地址; Docker Index:Use Docker Hub; Auto blocking enabled:勾选; Maximum component age:缓存时间自己定就好,默认 1440 为一天; Maximum metadata age:使用默认值即可; Blob store:选择之前创建的 docker-hub; Not found cache enabled:镜像没有找到是否缓存,默认缓存,按默认来; Not found cache TTL:缓存时长使用默认值即可; 没有提到的选项不用管,然后就创建完毕了。使用它可以从 docker hub 上下载镜像了,下载的方式下面会讲到。它应该还可以配置从 k8s.gcr.io 下载,这里的镜像是 docker hub 中没有的,有兴趣的可以试试。

接下来还得创建一个 repository,这个仓库用来分组,将上面两个仓库合并在一起,通过都一个端口来同时访问它们。

Create repository -> docker(group):

Name:docker-group; Online:勾选; HTTP:8082; Allow anonymous docker pull:我没有勾选,需要认证才能下载镜像; Enable Docker V1 API:如果你使用的 docker 版本够新的话,可以不勾选,我勾了; Blob store:它只是一个组,不存放任何东西,因此使用 default 即可; Member repositories:将 docker-private 和 docker-hub 放到右边; 创建完毕之后,nexus 会自动启动 8082 和 8083 端口。其中 8082 是用来 pull 私有仓库和 dockerhub 镜像的,8083 用来 push 镜像到私有仓库。现在要做的就是使用 nginx 为这些端口配置 https。

我们分别通过 2222 和 3333 端口来代理 8082 和 8083,并且在 2222 和 3333 上启用 https,因此 docker 需要连接 2222 和 3333 端口。

vim /etc/nginx/conf.d/nexus.conf server { listen 2222 ssl; server_name registry.ntpstat.com;ssl_certificate /etc/nginx/cert/fullchain.pem; ssl_certificate_key /etc/nginx/cert/privkey.pem; ssl_session_cache shared:SSL:50m; location / { proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_redirect http:// https://; proxy_pass http://127.0.0.1:8082; client_max_body_size 1024m; client_body_buffer_size 128k; }} server { listen 3333 ssl; server_name registry.ntpstat.com;ssl_certificate /etc/nginx/cert/fullchain.pem; ssl_certificate_key /etc/nginx/cert/privkey.pem; ssl_session_cache shared:SSL:50m; location / { proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_redirect http:// https://; proxy_pass http://127.0.0.1:8083; client_max_body_size 1024m; client_body_buffer_size 128k; }

} 复制代码 验证是否配置成功 配置完成之后就可以使用 docker login 命令验证了。在此之前,如果你系统上配置的 dns 无法解析 registry.ntpstat.com(替换成你的),你还需要将之添加到 hosts 文件中。这里略过,直接验证。

docker login -u admin -p admin123 registry.ntpstat.com:2222 docker login -u admin -p admin123 registry.ntpstat.com:3333

复制代码 只要出现 Login Succeeded 表示验证 ok,没出现自然就是失败了。

如果验证失败,你可以在 /etc/docker/daemon.json 中下面这行,IP 换成 nexus 的 ip 或主机名都可以。

“insecure-registries” : [“IP:8082”, “IP:8083”]

复制代码 文件修改完毕之后,使用 python -mjson.tool 点击小齿轮图标 -> System -> HTTP -> 勾选 HTTP proxy -> 添加主机和端口 -> Save。

这样一来,你 nexus 所有的 http/https 请求都会通过这个代理进行。当然,如果有不需要代理的 ip 你可以填在 Hosts to exclude from HTTP/HTTPS proxy 中。

现在你的 nexus 能上网了,那么先下载个 busybox 镜像吧。前面提到过,镜像下载使用 2222 端口。

docker login -u admin registry.ntpstat.com:2222docker pull registry.ntpstat.com:2222/busyboxUsing default tag: latest latest: Pulling from busybox 697743189b6d: Pull complete Digest: sha256:4415a904b1aca178c2450fd54928ab362825e863c0ad5452fd020e92f7a6a47e Status: Downloaded newer image for registry.ntpstat.com:2222/busybox:latest

复制代码 如果你允许匿名 pull 就不需要登录。

只需要将镜像名写在 registry.ntpstat.com:2222/ 的后面即可,nexus 本地如果没有,就会去 dockerhub 上下载。当然,这么下载下来的镜像名自然也就是很长的一串了。

上传镜像 上传镜像使用 3333 端口,我们将刚刚下载下来的镜像上传。

docker login -u admin registry.ntpstat.com:3333 docker tag registry.ntpstat.com:2222/busybox registry.ntpstat.com:3333/busybox:test docker push registry.ntpstat.com:3333/busybox:testThe push refers to repository [registry.ntpstat.com:3333/busybox] adab5d09ba79: Pushed test: digest: sha256:4415a904b1aca178c2450fd54928ab362825e863c0ad5452fd020e92f7a6a47e size: 527

复制代码 push 必须登录。我这里给 busybox 加了个 tag,只是为了测试,没有任何作用。那么关于 nexus 作为 docker 仓库的内容也就这么多了,接下来配置 harbor。

harbor 在安装使用 harbor 之前,如果你的 harbor 和 nexus 都安装在同一台服务器上,你需要将代理 nexus 的 nginx 的 80 和 443 端口改掉,因为 harbor 会监听这两个端口。其实这样一来的话,nginx 的 80 和 443 都可以不用了,直接使用 nexus 的 8081 访问即可。

安装 harbor 是 VMware 开源的 registry,也是一款大众熟知的产品,它的 GitHub 地址在此。它的安装只能通过 docker 进行,因此你得首先安装 docker。

yum install -y yum-utils device-mapper-persistent-data lvm2 cat > /etc/yum.repos.d/docker-ce.repo


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3